package com.bears.god.sup.servlet.toAction;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.bears.god.sup.model.Bill;
import com.bears.god.sup.model.Income;
import com.bears.god.sup.model.Vendor;
import com.bears.god.sup.service.IBillService;
import com.bears.god.sup.service.ServiceDAOFactory;

@WebServlet("/toBill")
public class toBillManageServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3441L;
	
	public static final String METHOD_BILL = "0";
	public static final String METHOD_FINISH = "1";
	public static final String METHOD_ADD = "2";
	public static final String METHOD_LIST = "3";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		String method = req.getParameter("method");

		if (method != null) {
			switch (method) {
			case toBillManageServlet.METHOD_BILL:
				showVendor(req, resp);
				break;
			case toBillManageServlet.METHOD_FINISH:
				finish(req, resp);
				break;
			case toBillManageServlet.METHOD_ADD:
				add(req, resp);
				break;
			case toBillManageServlet.METHOD_LIST:
				list(req, resp);
				break;
			}

		}
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) {
		try {
			List<Bill> billAlllist = ServiceDAOFactory.getBillDAO().findAll();
			req.setAttribute("billAlllist", billAlllist);
			req.getRequestDispatcher("WEB-INF/view/cashier/billAll.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private void finish(HttpServletRequest req, HttpServletResponse resp) {
		Bill bill = null;
		try {
			bill = new Bill();
			String VendorName = req.getParameter("vendorName");
			String BillDate = req.getParameter("billDate");
			String DueDate = req.getParameter("dueDate");
			
			
			bill.setVendorName(VendorName);
			bill.setDate(BillDate);
			bill.setDueDate(DueDate);
			
			IBillService service = ServiceDAOFactory.getBillDAO();
			service.save(bill);
			req.setAttribute("bill", bill);
			req.getRequestDispatcher("/listbill?method=0").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private void showVendor(HttpServletRequest req, HttpServletResponse resp) {
		List<Vendor> vondors;

		try {
			vondors = com.bears.god.sup.service.ServiceDAOFactory.getVendorDAO().findAll();
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
			com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();

			for (Vendor vondor : vondors) {
				com.alibaba.fastjson.JSONObject json1 = (com.alibaba.fastjson.JSONObject) JSON.toJSON(vondor);
				array.add(json1);
			}
			json.put("vendors", array);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getRequestDispatcher("WEB-INF/view/cashier/addbill.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
